package me.cxlr.shaopinghelper.util;

import android.os.Parcelable;

import com.tencent.mmkv.MMKV;

import java.util.Collections;
import java.util.Set;

import me.cxlr.shaopinghelper.MyApplication;

/**
 * MMKV
 *
 * @author Panini
 */
public final class MmkvUtil {
    private static MMKV mmkv = null;

    private MmkvUtil() {
    }

    /**
     * 初始化MMKV
     */
    public static void initMmkv() {
        MMKV.initialize(MyApplication.getInstance());
        mmkv = MMKV.defaultMMKV();
    }

    /**
     * 保存
     *
     * @param key    键
     * @param object 值
     */
    public static void encode(String key, Object object) {
        if (object instanceof String) {
            mmkv.encode(key, String.valueOf(object));
        } else if (object instanceof Integer) {
            mmkv.encode(key, (Integer) object);
        } else if (object instanceof Boolean) {
            mmkv.encode(key, (Boolean) object);
        } else if (object instanceof Float) {
            mmkv.encode(key, (Float) object);
        } else if (object instanceof Long) {
            mmkv.encode(key, (Long) object);
        } else if (object instanceof Double) {
            mmkv.encode(key, (Double) object);
        } else if (object instanceof byte[]) {
            mmkv.encode(key, (byte[]) object);
        } else {
            mmkv.encode(key, object.toString());
        }
    }

    /**
     * 保存
     *
     * @param key 键
     * @param set set
     */
    public static void encodeSet(String key, Set<String> set) {
        mmkv.encode(key, set);
    }

    /**
     * 保存
     *
     * @param key        键
     * @param parcelable parcelable
     */
    public static void encodeParcelable(String key, Parcelable parcelable) {
        mmkv.encode(key, parcelable);
    }

    /**
     * 根据key查询
     *
     * @param key 键
     * @return value
     */
    public static Integer decodeInt(String key) {
        return mmkv.decodeInt(key, 0);
    }

    /**
     * 根据key查询
     *
     * @param key      键
     * @param defValue 默认值
     * @return value
     */
    public static Integer decodeInt(String key, Integer defValue) {
        return mmkv.decodeInt(key, defValue);
    }

    /**
     * 根据key查询
     *
     * @param key 键
     * @return value
     */
    public static Double decodeDouble(String key) {
        return mmkv.decodeDouble(key, 0.0d);
    }

    /**
     * 根据key查询
     *
     * @param key      键
     * @param defValue 默认值
     * @return value
     */
    public static Double decodeDouble(String key, Double defValue) {
        return mmkv.decodeDouble(key, defValue);
    }

    /**
     * 根据key查询
     *
     * @param key 键
     * @return value
     */
    public static Long decodeLong(String key) {
        return mmkv.decodeLong(key, 0L);
    }

    /**
     * 根据key查询
     *
     * @param key      键
     * @param defValue 默认值
     * @return value
     */
    public static Long decodeLong(String key, Long defValue) {
        return mmkv.decodeLong(key, defValue);
    }

    /**
     * 根据key查询
     *
     * @param key 键
     * @return value
     */
    public static Boolean decodeBoolean(String key) {
        return mmkv.decodeBool(key, false);
    }

    /**
     * 根据key查询
     *
     * @param key      键
     * @param defValue 默认值
     * @return value
     */
    public static Boolean decodeBoolean(String key, Boolean defValue) {
        return mmkv.decodeBool(key, defValue);
    }

    /**
     * 根据key查询
     *
     * @param key 键
     * @return value
     */
    public static Float decodeFloat(String key) {
        return mmkv.decodeFloat(key, 0F);
    }

    /**
     * 根据key查询
     *
     * @param key      键
     * @param defValue 默认值
     * @return value
     */
    public static Float decodeFloat(String key, Float defValue) {
        return mmkv.decodeFloat(key, defValue);
    }

    /**
     * 根据key查询
     *
     * @param key 键
     * @return value
     */
    public static byte[] decodeBytes(String key) {
        return mmkv.decodeBytes(key);
    }

    /**
     * 根据key查询
     *
     * @param key 键
     * @return value
     */
    public static String decodeString(String key) {
        return mmkv.decodeString(key, "");
    }

    /**
     * 根据key查询
     *
     * @param key      键
     * @param defValue 默认值
     * @return value
     */
    public static String decodeString(String key, String defValue) {
        return mmkv.decodeString(key, defValue);
    }

    /**
     * 根据key查询
     *
     * @param key 键
     * @return value
     */
    public static Set<String> decodeStringSet(String key) {
        return mmkv.decodeStringSet(key, Collections.emptySet());
    }

    /**
     * 根据key查询
     *
     * @param key 键
     * @return value
     */
    public static Parcelable decodeParcelable(String key) {
        return mmkv.decodeParcelable(key, null);
    }

    /**
     * 根据key移除
     *
     * @param key 键
     */
    public static void removeKey(String key) {
        mmkv.removeValueForKey(key);
    }

    /**
     * 清除所有key
     */
    public static void clearAll() {
        mmkv.clearAll();
    }

    /**
     * 是否包含某个key
     *
     * @param key 键
     */
    public static boolean containsKey(String key) {
        return mmkv.containsKey(key);
    }

}
